package br.com.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.mail.iap.Response;

import br.com.dao.UsuarioDAO;
import br.com.model.Usuario;


public class AutenticadorController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public AutenticadorController() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//Captura dados da tela
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		
		//Constroi Objeto Usuario para Consulta
		Usuario usuario = new Usuario();
		usuario.setLogin(login);
		usuario.setSenha(senha);
		
		//Buscar no banco
		UsuarioDAO usuarioDAO = new UsuarioDAO();
		
		Usuario usuRetorno = usuarioDAO.autenticar(usuario);
		
		if(usuRetorno!=null){
			//Criando sessao
			HttpSession sessao = request.getSession();
			sessao.setAttribute("usuLogado", usuRetorno);
			
			//Encaminhando ao Index
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else{
			response.sendRedirect("login.html");
		}
			
	}

}
